#include "UI_Button.h"

#include "GL/glut.h"
#include <GL/gl.h>
#include <GL/glu.h>

UI_Button::UI_Button(void) {
	position.set(0, 0, 0);
	orientation.set(0, 0, -1);
	up.set(0, 1, 0);
	scale = 1;
	height = width = 0;
	colour.R = colour.G = colour.B = 1;

	texture = NULL;
}

UI_Button::UI_Button(const Texture2D * assigned_texture) {
	position.set(0, 0, 0);
	orientation.set(0, 0, -1);
	up.set(0, 1, 0);
	scale = 1;
	height = width = 0;
	colour.R = colour.G = colour.B = 0;

	texture = (Texture2D*)assigned_texture;
}

UI_Button::~UI_Button(void) {
}

void UI_Button::AssignTexture(const Texture2D * assigned_texture) {
	texture = (Texture2D*)assigned_texture;
}

void UI_Button::Draw() {
	if (texture != NULL)
		texture->Draw(position, orientation, up, scale, width, height);
	else 
		NoTextureDraw(position, orientation, up, scale, width, height);
}

void UI_Button::DefaultDraw() {
	if (texture != NULL)
		texture->Draw();
	else 
		NoTextureDefaultDraw();
}

void UI_Button::NoTextureDraw(VectorXYZ position, VectorXYZ orientation, VectorXYZ up, float scale, float width, float height) {
	// this is the origin point (the position of the button)
	VectorXYZ point_TL; // Top Left 
	
	VectorXYZ point_BL; // Bottom Left
	VectorXYZ point_BR; // Bottom Right
	VectorXYZ point_TR; // Top Right

	point_TL += position;
	point_BL = point_TL - ((height*scale) * up);
	point_TR = point_TL + ((width*scale) * (orientation % up));
	point_BR = point_TR + point_BL - point_TL;

	glColor3f(colour.R, colour.G, colour.B);

	// draw the image as a quad the size of the first loaded image
	glBegin(GL_QUADS);
		glVertex3f	(point_TL.x, point_TL.y, point_TL.z); // Top Left
		glVertex3f	(point_BL.x, point_BL.y, point_BL.z); // Bottom Left
		glVertex3f	(point_BR.x, point_BR.y, point_BR.z); // Bottom Right
		glVertex3f	(point_TR.x, point_TR.y, point_TR.z); // Top Right
	glEnd();
}

void UI_Button::NoTextureDefaultDraw() {

}